package com.gitee.xmhzzz.component.spring.cloud.component.dao;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.gitee.xmhzzz.component.spring.cloud.component.dao.interceptor.AutoRoutingInterceptor;
import com.gitee.xmhzzz.component.spring.cloud.component.dao.interceptor.SqlCheckInterceptor;
import com.gitee.xmhzzz.component.spring.cloud.component.dao.interceptor.SqlCheckMybatisInterceptor;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

@EnableTransactionManagement
@Configuration
@MapperScan({"com.gitee.xmhzzz.**.mapper*"})
public class DaoConfiguration
{

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false
     * 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件不需要指定数据库dialect，会自动获取
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    @Bean
    @ConditionalOnProperty(value = "xmhzzz.auto.routing.enable", havingValue = "true")
    public AutoRoutingInterceptor autoRoutingInterceptor(){
        return new AutoRoutingInterceptor();
    }

    @Bean
    public SqlCheckInterceptor sqlCheckInterceptor(){
        return new SqlCheckInterceptor();
    }


    @Bean
    public SqlCheckMybatisInterceptor SqlCheckMybatisInterceptor(@Autowired SqlCheckInterceptor sqlCheckInterceptor){
        return new SqlCheckMybatisInterceptor(sqlCheckInterceptor);
    }

    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties pro = new Properties();
        pro.setProperty("offsetAsPageNum", "true");
        pro.setProperty("rowBoundsWithCount", "true");
        pro.setProperty("reasonable", "true");
        pageHelper.setProperties(pro);
        return pageHelper;
    }


}
